﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Drawing;
using System.Linq;
using System.Drawing.Drawing2D;

namespace Shapefile
{
    public class ESRIPolygon : ESRIShape
    {
        private RectangleD boundingBox_;
        private List<PointD[]> parts_;

        protected internal ESRIPolygon(int recordNumber, StringDictionary metadata, IDataRecord dataRecord, byte[] shapeData)
            : base(ShapeType.Polygon, recordNumber, metadata, dataRecord)
        {
            ParsePolyLineOrPolygon(shapeData, out boundingBox_, out parts_);
        }

        public RectangleD BoundingBox
        {
            get { return boundingBox_; }
        }

        public List<PointD[]> Parts
        {
            get { return parts_; }
        }

        public override void Render(Graphics g)
        {
            float sx = g.Transform.Elements[0];
            float sy = g.Transform.Elements[3];
            float pen_width = 1.0f / (sx - sy) * 2;
            Pen pen = new Pen(Color.Blue, pen_width);
            Brush brush = new SolidBrush(Color.AliceBlue);
            for (int i=0;i<parts_.Count;i++)
            {
                GraphicsPath path = new GraphicsPath();
                PointF[] temp=new PointF[parts_[i].Length];
                for(int j=0;j<parts_[i].Length;j++)
                {
                    temp[j].X = Convert.ToSingle(parts_[i][j].X);
                    temp[j].Y = Convert.ToSingle(parts_[i][j].Y);
                }

                path.AddPolygon(temp);
                g.FillPath(brush, path);
                g.DrawPath(pen,path);
                
            }
            
        }
    }
}
